package com.st.activeMQ.listener;

import com.st.pojo.BookBean;
import com.st.service.AMQProductionService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.*;


/**
 * MQ消费者，商家消费MQ中的消息，处理MQ订单
 */

public class QueueListener {
    @Resource
    private AMQProductionService amqProductionService;


    @Transactional
    public void disPalyOrder(BookBean bookBean) throws JMSException {
        /**
         * 从MQ当中获取消息数据，根据业务逻辑，针对数据库操作
         */
        System.out.println("商家接到订单的消息" + bookBean);
        int succ = amqProductionService.addOrder(bookBean);
        if (succ > 0) {
            System.out.println("===========商家接到订单的消息" + bookBean);
        }
    }


//    public void xiaofei() throws JMSException {
//        final String url="tcp://192.168.200.216:61616/";
//        final String queueName="spring-queue";
//        //1.创建ConnectionFactory
//        ConnectionFactory acf = new org.apache.activemq.ActiveMQConnectionFactory(url);
//
//        //2.创建Connection
//        Connection createConnection =  acf.createConnection();
//
//        //3,启动连接
//        createConnection.start();
//
//        //4.创建会话
//        Session session = createConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//
//        //5创建一个目标
//        Destination des = session.createQueue(queueName);
//
//        //6.创建一个消费者
//        MessageConsumer consumer = session.createConsumer(des);
//
//        //7.创建一个监听器
//        consumer.setMessageListener(new MessageListener() {
//
//            @Override
//            public void onMessage(Message message) {
//                TextMessage textMessage=(TextMessage) message;
//                try {
//                    System.out.println("消费者接受消息"+textMessage.getText());
//                } catch (JMSException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//        });
//
//    }

}
